<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>Promise异步</p>
    <p>参考：https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise</p>
    <script>
        const promise1 = new Promise(function(resolve, reject) {
            setTimeout(function() {//setTimeout模拟一些耗时任务
                resolve('foo');

            }, 3000);
        });

        promise1.then(function(value) {
        console.log(value);
        // expected output: "foo"
        })
        ;
        console.log(promise1);



        
        const promise2 = new Promise(function(resolve, reject) {
            setTimeout(function() {//setTimeout模拟一些耗时任务
                reject("failed")
            }, 300);
        });

        promise2.then(function(value) {
            console.log(value);       
        })
        .catch(
        // 记录失败原因
        (reason) => {
            console.log(reason);//打印failed
        })
        ;
    </script>
</body>
</html>